/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Controleur.Util;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * Affiche les champs d'edition d'un contact.
 * @author Jo
 */
public class PanelEdition extends PanelDroiteAbstract {
    
    String nouveauCheminIcone;
    
    public PanelEdition() {
        nouveauCheminIcone = "";
        
        labelNom = new JTextField(stringInitial);
        labelPrenom = new JTextField(stringInitial);
        labelTelMobile = new JTextField(stringInitial);
        labelTelMaison = new JTextField(stringInitial);
        labelTelBureau = new JTextField(stringInitial);
        labelAdresse = new JTextField(stringInitial);
        labelSite = new JTextField(stringInitial);
        labelEmail = new JTextField(stringInitial);
        
        this.init();
        
        labelIcone.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("click!");
                //nouveauCheminIcone="";
                
                
                JFrame fenFile = new JFrame();
                
                JFileChooser chooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    "JPG & GIF Images", "jpg", "gif");
                chooser.setFileFilter(filter);
                int returnVal = chooser.showOpenDialog(fenFile);
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                        nouveauCheminIcone = Util.path_to_dossier + Util.FILE_SEPARATOR + idContact;
                        copierFichier(new File(chooser.getSelectedFile().getCanonicalPath()), new File(nouveauCheminIcone));
                        
                        labelIcone.setIcon(Controleur.Util.creerIcone(nouveauCheminIcone));
                        
                    } catch (IOException ex) { Logger.getLogger(PanelEdition.class.getName()).log(Level.SEVERE, null, ex); }
                }
            }
            
            private void copierFichier(File source, File dest) throws IOException {
                InputStream is = null;
                OutputStream os = null;
                try {
                    is = new FileInputStream(source);
                    os = new FileOutputStream(dest);
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = is.read(buffer)) > 0) {
                        os.write(buffer, 0, length);
                    }
                } finally {
                    is.close();
                    os.close();
                }
            }
        });
    }
    
    public String getNom(){
        return ((JTextField) labelNom).getText();
    }
    public String getPrenom(){
        return ((JTextField) labelPrenom).getText();
    }
    public String getTelMobile(){
        return ((JTextField) labelTelMobile).getText();
    }
    public String getTelMaison(){
        return ((JTextField) labelTelMaison).getText();
    }
    public String getTelBureau(){
        return ((JTextField) labelTelBureau).getText();
    }
    public String getSiteWeb(){
        return ((JTextField) labelSite).getText();
    }
    public String getEmail(){
        return ((JTextField) labelEmail).getText();
    }
    
}
